package www.wen.com.common.lib.base.module

import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import www.wen.com.common.R
import www.wen.com.common.lib.data.entities.*
import www.wen.com.common.lib.data.repository.AppDataManager
import www.wen.com.common.lib.extension.getString
import www.wen.com.common.lib.extension.verificationText

/**
 * @author WEN
 * @Description:
 * @date 2020/11/2 17:35
 */
open class BaseCountDownModel constructor(appDataManager: AppDataManager) : BaseApiViewModel(appDataManager), LifecycleObserver {

    val mTiming = MutableLiveData(0)

    val mEnabled = MutableLiveData(false)

    open val mEnabledSubmit = MutableLiveData(true)

    val mVerificationCodeText = MutableLiveData(0.verificationText())

    val mVerificationCode = MutableLiveData("")

    val mPassWord = MutableLiveData("")

    val mPhone = MutableLiveData("")

    fun onPhoneTextChanged(textChange: CharSequence) {
        mEnabled.value = enableSendVerificationCode()
        switchEnabledSubmit(textChange)
    }

    fun onVerificationCodeChanged(textChange: CharSequence) {
        switchEnabledSubmit(textChange)
    }

    fun onPasswordChanged(textChange: CharSequence) {
        switchEnabledSubmit(textChange)
    }

    fun enableSendVerificationCode(): Boolean {
        return mPhone.value!!.isNotBlank() &&
                mPhone.value!!.length >= 5 &&
                mTiming.value == 0
    }

    open fun switchEnabledSubmit(textChange: CharSequence) {}

    /**
     * 获取验证码
     */
    fun requestVerifyCode(
            requestVerifyBody: RequestVerifyBody,
            action: (() -> Unit)? = null,
    ) {
        viewModelScope.launch {
            loading.value = UiStateCustom(
                    stateData = LoadingDialog(
                            getString(R.string.verification_code_sending),
                            LoadingDialogStateType.LOADING
                    )
            )
            val result = appDataManager.requestVerifyCode(requestVerifyBody)
            if (result is Result.Success) {
                loading.value = UiStateCustom(
                        stateData = LoadingDialog(
                                tip = getString(R.string.sent_successfully),
                                loadingDialogStateType = LoadingDialogStateType.SUCCESS,
                                isShowDialogTip = false,
                                isShowToastTip = true,
                                action = action
                        )
                )
            } else if (result is Result.Error) {
                loading.value = UiStateCustom(
                        stateData = LoadingDialog(
                                tip = result.exception.message!!,
                                loadingDialogStateType = LoadingDialogStateType.ERROR,
                                isShowDialogTip = false,
                                isShowToastTip = true
                        )
                )
            }
        }
    }


}